import {AfterViewInit, Component, OnInit, OnDestroy, ViewEncapsulation} from '@angular/core';
import {Email} from '../../../globalModel/Email';
import {EmailService} from '../../../service/email.service';
import {AdminService} from '../../../service/admin.service';
import {MessagerService} from 'ng-easyui/components/messager/messager.service';
import {Subscription} from 'rxjs';

@Component({
  selector: 'app-email',
  templateUrl: './email.component.html',
  styleUrls: ['./email.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class EmailComponent implements OnInit, OnDestroy, AfterViewInit {
  componentTitle = '邮件管理'; // 组件标题
  adminVariableSubscription: Subscription = null; // 订阅对象
  // eui-datagrid（数据表格）
  loading = true; // 开启加载提示
  loadMsg = '正在加载..';
  total = 0;
  data = [];
  pageNumber = 1;
  pageSize = 20;
  pageOptions = { // 分页导航条参数设置
    pageList: [10, 20, 30, 40, 50],
    displayMsg: '当前 {from} 到 {to} , 共 {total} 条',
    layout: ['list', 'sep', 'first', 'prev', 'sep', 'tpl', 'sep', 'next', 'last', 'sep', 'refresh', 'sep', 'links', 'info']
  };
  selectRow = []; // 存储点击某一行后选中的行（数组）
  itemForPage: Email = {}; // 分页参数对象,双向数据绑定

  /**
   * 构造函数
   */
  constructor(
    private service: EmailService,
    private adminService: AdminService,
    private messagerService: MessagerService) {
    /**
     * 监听父组件推送的消息
     */
    this.adminVariableSubscription = adminService.adminObservable.subscribe(adminVariable => {
      /**
       * 删除回调事件
       */
      if (adminVariable.componentName === 'admin' && adminVariable.deleteSuccess !== null) {
        this.selectRow = []; // 取消选中的行
        if (adminVariable.deleteSuccess) {
          // 删除成功
          this.onPageChange({pageNumber: this.pageNumber, pageSize: this.pageSize}); // 重置当前页数据
        }
      }
    });
  }

  /**
   * 初始化函数
   */
  ngOnInit() {
  }

  /**
   * 组件销毁
   */
  ngOnDestroy() {
    if (this.adminVariableSubscription) {
      this.adminVariableSubscription.unsubscribe(); // 取消订阅,防止内存泄漏
    }
  }

  /**
   * 在组件相应的视图初始化之后调用
   */
  ngAfterViewInit() {
    this.onPageChange({pageNumber: 1, pageSize: this.pageSize});
  }

  /**
   * 分页插件
   * @param event
   */
  onPageChange(event) {
    if (event.pageNumber > 0) {
      this.pageNumber = event.pageNumber;
      this.pageSize = event.pageSize;
      this.page();
    }
  }

  /**
   * 分页查询
   */
  page() {
    this.service.page(this.itemForPage, this.pageNumber, this.pageSize).subscribe(responseJson => {
      this.loading = false; // 关闭加载提示
      if (responseJson.code === 0) {
        this.data = responseJson.data.list;
        this.total = responseJson.data.total;
      }
    });
  }

  /**
   * 打开删除弹框
   */
  onOpenDeleteDlg(param): void {
    let id;
    if (param === 'deleteMore') {
      // 删除所选
      if (this.selectRow.length > 0) {
        id = this.selectRow.map(row => row.id).join(',');
        this.adminService.pushAdminVariable({componentName: 'email', deleteDlgClosed: false, id: id});
      } else {
        this.messagerService.alert({title: '温馨提示', msg: '请先选中需要删除的数据!', ok: '确定'});
      }
    } else {
      // 删除所有
      id = 'all';
      this.adminService.pushAdminVariable({componentName: 'email', deleteDlgClosed: false, id: id});
    }
  }

  /**
   * 刷新首页数据
   */
  onReLoad(): void {
    this.selectRow = [];
    this.onPageChange({pageNumber: 1, pageSize: this.pageSize});
  }

}
